package com.example.ssm.service;

import com.example.ssm.dao.BasicDao;
import com.example.ssm.pojo.BasicInfo;
import com.example.ssm.pojo.basicinfo.delete.MessageModel_BasicDelete;
import com.example.ssm.pojo.basicinfo.query.MessageModel_BasicQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Package: com.example.ssm.service
 * @ClassName: BasicService
 * @Author: 76470
 * @CreateTime: 2021/6/10 16:59
 * @Description:
 */
@Service
public class BasicService {
	@Autowired
	private BasicDao basicDao;
	private Integer x, y;
	//查询服务
	public MessageModel_BasicQuery BasicQueryService(String pageSize, String pageNum, String name, String category){
		//如果name和category参数为空
		if((name == null||name.length () == 0) && (category == null||category.length () == 0)){
			System.out.println ("Service接收到pageSize:"+pageSize+", pageNum:"+pageNum);//test
			x = 0 + (Integer.valueOf( pageNum ) - 1) * Integer.valueOf ( pageSize );
			y = Integer.valueOf ( pageSize );
			System.out.println ("起始值："+x+",加载值："+y);//test
			MessageModel_BasicQuery messageModel_basicQuery = new MessageModel_BasicQuery ();
			messageModel_basicQuery.setList ( basicDao.findAllByPage (x, y) );
			messageModel_basicQuery.setCount ( basicDao.countRow () );
			System.out.println ("返回值："+basicDao.findAllByPage (x, y));//test
			return messageModel_basicQuery;
		}
		//如果name不为空，category为空
		else if((name != null||name.length () != 0) && (category == null||category.length () == 0)){
			System.out.println ("Service接收到pageSize:"+pageSize+", pageNum:"+pageNum+", name:"+name);//test
			x = 0 + (Integer.valueOf( pageNum ) - 1) * Integer.valueOf ( pageSize );
			y = Integer.valueOf ( pageSize );
			System.out.println ("起始值："+x+",加载值："+y);//test
			MessageModel_BasicQuery messageModel_basicQuery = new MessageModel_BasicQuery ();
			messageModel_basicQuery = JudgePage ( pageNum, y, basicDao.findAllByName ( name ) );
			System.out.println ("返回值:"+messageModel_basicQuery);//test
//			messageModel_basicQuery.setList ( basicDao.findAllByName ( name ) );
//			messageModel_basicQuery.setCount ( basicDao.countRow () );
//			System.out.println ("返回值："+basicDao.findAllByName ( name ));//test
			return messageModel_basicQuery;
		}
		//如果name为空，category不为空
		else if((name == null||name.length () == 0) && (category != null||category.length () != 0)){
			System.out.println ("Service接收到pageSize:"+pageSize+", pageNum:"+pageNum+", category:"+category);//test
			x = 0 + (Integer.valueOf( pageNum ) - 1) * Integer.valueOf ( pageSize );
			y = Integer.valueOf ( pageSize );
			System.out.println ("起始值："+x+",加载值："+y);//test
			MessageModel_BasicQuery messageModel_basicQuery = new MessageModel_BasicQuery ();
			messageModel_basicQuery = JudgePage ( pageNum, y, basicDao.findAllByCategory ( category ) );
			System.out.println ("返回值:"+messageModel_basicQuery);//test
//			messageModel_basicQuery.setList ( basicDao.findAllByCategory ( category ) );
//			messageModel_basicQuery.setCount ( basicDao.countRow () );
//			System.out.println ("返回值："+basicDao.findAllByCategory ( category ));//test
			return messageModel_basicQuery;
		}
		//如果name和category都不为空
		else{
			System.out.println ("Service接收到pageSize:"+pageSize+", pageNum:"+pageNum+", name:"+name+", category:"+category);//test
			x = 0 + (Integer.valueOf( pageNum ) - 1) * Integer.valueOf ( pageSize );
			y = Integer.valueOf ( pageSize );
			System.out.println ("起始值："+x+",加载值："+y);//test
			MessageModel_BasicQuery messageModel_basicQuery = new MessageModel_BasicQuery ();
			messageModel_basicQuery = JudgePage ( pageNum, y, basicDao.findAllByNameCategory ( name, category ) );
			System.out.println ("返回值:"+messageModel_basicQuery);//test
//			messageModel_basicQuery.setList ( basicDao.findAllByNameCategory ( name, category ) );
//			messageModel_basicQuery.setCount ( basicDao.countRow () );
//			System.out.println ("返回值："+basicDao.findAllByNameCategory ( name, category ));//test
			return messageModel_basicQuery;
		}
	}

	//分页判断
	public MessageModel_BasicQuery JudgePage(String pageNum, Integer y, List<BasicInfo> list ){
		MessageModel_BasicQuery messageModel_basicQuery = new MessageModel_BasicQuery ();
		System.out.println ("JudgePage接收到的pageNum:"+pageNum+", y:"+y+", list:"+list);//test
		Integer len = list.size ();//获取当前泛型中的元素数目
		Integer maxSize = y;//每页的最大容量
		Integer exa = len/y;//整数页数目
		Integer rel = len%y;//最后一页记录的数目
		Integer tel;//总页数
		Integer num;//当前页码内记录的数目
		//判断总页数
		if(rel != 0){
			tel = exa + 1;
		}
		else{
			tel = exa;
		}
		//判断当前页面内的总记录条数
		if(Integer.valueOf ( pageNum ) <= exa ){
			num = maxSize;
		}
		else{
			num = rel;
		}

		BasicInfo[] info = new BasicInfo[num];
//		List<BasicInfo> info = new List<BasicInfo> () {
//			@Override
//			public int size () {
//				return 0;
//			}
//
//			@Override
//			public boolean isEmpty () {
//				return false;
//			}
//
//			@Override
//			public boolean contains ( Object o ) {
//				return false;
//			}
//
//			@Override
//			public Iterator<BasicInfo> iterator () {
//				return null;
//			}
//
//			@Override
//			public Object[] toArray () {
//				return new Object[0];
//			}
//
//			@Override
//			public <T> T[] toArray ( T[] a ) {
//				return null;
//			}
//
//			@Override
//			public boolean add ( BasicInfo basicInfo ) {
//				return false;
//			}
//
//			@Override
//			public boolean remove ( Object o ) {
//				return false;
//			}
//
//			@Override
//			public boolean containsAll ( Collection<?> c ) {
//				return false;
//			}
//
//			@Override
//			public boolean addAll ( Collection<? extends BasicInfo> c ) {
//				return false;
//			}
//
//			@Override
//			public boolean addAll ( int index, Collection<? extends BasicInfo> c ) {
//				return false;
//			}
//
//			@Override
//			public boolean removeAll ( Collection<?> c ) {
//				return false;
//			}
//
//			@Override
//			public boolean retainAll ( Collection<?> c ) {
//				return false;
//			}
//
//			@Override
//			public void clear () {
//
//			}
//
//			@Override
//			public BasicInfo get ( int index ) {
//				return null;
//			}
//
//			@Override
//			public BasicInfo set ( int index, BasicInfo element ) {
//				return null;
//			}
//
//			@Override
//			public void add ( int index, BasicInfo element ) {
//
//			}
//
//			@Override
//			public BasicInfo remove ( int index ) {
//				return null;
//			}
//
//			@Override
//			public int indexOf ( Object o ) {
//				return 0;
//			}
//
//			@Override
//			public int lastIndexOf ( Object o ) {
//				return 0;
//			}
//
//			@Override
//			public ListIterator<BasicInfo> listIterator () {
//				return null;
//			}
//
//			@Override
//			public ListIterator<BasicInfo> listIterator ( int index ) {
//				return null;
//			}
//
//			@Override
//			public List<BasicInfo> subList ( int fromIndex, int toIndex ) {
//				return null;
//			}
//		};
		
		System.out.println ("len:"+len+", maxSize"+maxSize);//test
		//如果当前查询结果返回为空
		if(len == 0){
			messageModel_basicQuery.setCount ( 0 );
			return messageModel_basicQuery;
		}
		else{
			if(Integer.valueOf ( pageNum ) > tel){
				return messageModel_basicQuery;
			}
			else{
				
				for(int i = 0; i < num; i++){
					if((Integer.valueOf ( pageNum ) - 1) * maxSize + i <= len){
						int temp = (Integer.valueOf ( pageNum ) - 1) * maxSize + i;
						BasicInfo basicInfo = list.get ( temp );
						info[i] = basicInfo;

					}
				}
				messageModel_basicQuery.setList ( Arrays.asList ( info ) );
				messageModel_basicQuery.setCount ( len );
				System.out.println ("JudgePage的返回值:"+messageModel_basicQuery);
				return messageModel_basicQuery;
			}
			
		}
	
	}
	
	//删除服务
	public MessageModel_BasicDelete BasicDeleteService(String id){
		MessageModel_BasicDelete messageModel_basicDelete = new MessageModel_BasicDelete ();
		System.out.println ("Service接收到id:"+id);//test
		messageModel_basicDelete.setCode ( basicDao.deleteById ( id ) );
		System.out.println ("返回值："+messageModel_basicDelete);
		return messageModel_basicDelete;
	}
	
}
